/*
 * Copyright 2011 J. Auer, J. Andert, F.-J. Jendrusch, U. Celebi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.fuberlin.replication.storage.jdbc.sqlite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Create a connection to the SQLite Database using JDBC and create the needed tables.
 */
public class SQLiteDatabaseHelper {
	private Connection connection = null;
	private String databaseName;

	/**
	 * Constructor.
	 */
	public SQLiteDatabaseHelper() {
		this(null);
	}

	/**
	 * Constructor.
	 * 
	 * @param dbName Optional - file name for the database.
	 */
	public SQLiteDatabaseHelper(String dbName) {
		if (dbName == null)
			databaseName = "replication.db";
		else
			databaseName = dbName + ".db";

		try {
			Class.forName("org.sqlite.JDBC");
			this.connection = DriverManager.getConnection("jdbc:sqlite:"
					+ databaseName);

			create();
			pragma();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void create() throws SQLException {
		Statement s = connection.createStatement();

		s.executeUpdate("CREATE TABLE if not exists version (version INTEGER);");

		s.executeUpdate("CREATE TABLE if not exists subset ("
				+ "id INTEGER PRIMARY KEY ASC AUTOINCREMENT" + ");");

		s.executeUpdate("CREATE TABLE if not exists knowledge ("
				+ "subset_id INTEGER REFERENCES subset (id) ON UPDATE CASCADE ON DELETE CASCADE, "
				+ "replica TEXT, " + "version INTEGER, "
				+ "PRIMARY KEY (subset_id, replica) ON CONFLICT REPLACE" + ");");

		s.executeUpdate("CREATE TABLE if not exists metadata ("
				+ "name TEXT PRIMARY KEY ON CONFLICT REPLACE, "
				+ "subset_id INTEGER REFERENCES subset (id) ON UPDATE CASCADE ON DELETE RESTRICT, "
				+ "replica TEXT, " + "version INTEGER, " + "timestamp INTEGER"
				+ ");");
		s.close();
	}

	private void pragma() throws SQLException {
		Statement s = connection.createStatement();
		s.execute("PRAGMA foreign_keys = ON");
		s.close();
	}

	/**
	 * Drop tables from DB.
	 * 
	 * @throws SQLException If tables could not be dropped
	 */
	public void drop() throws SQLException {
		Statement s = connection.createStatement();
		s.executeUpdate("DROP TABLE if exists version;");
		s.executeUpdate("DROP TABLE if exists subset");
		s.executeUpdate("DROP TABLE if exists knowledge");
		s.executeUpdate("DROP TABLE if exists metadata");
		s.close();
	}

	/**
	 * Getter for the DB connection.
	 * 
	 * @return SQLite DB connection
	 */
	public Connection getConnection() {
		return this.connection;
	}
}